package com.qeeyou.apps.android.craftsman.view;

import android.content.Context;
import android.content.res.ColorStateList;
import android.content.res.TypedArray;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.qeeyou.apps.android.craftsman.R;

/**
 * 自定义TabbarButton
 * Created by xuchengxiong on 2017/12/16.
 */

public class TabbarButton extends LinearLayout {

    private LinearLayout view;
    private ImageView imageView;
    private TextView textView;

    private int image;
    private String text;
    private ColorStateList textColors;

    public TabbarButton(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.tabbarButton);
        image = typedArray.getResourceId(R.styleable.tabbarButton_image, R.drawable.ic_tab_home_off);
        text = typedArray.getString(R.styleable.tabbarButton_text);
        textColors = typedArray.getColorStateList(R.styleable.tabbarButton_textColor);
        //TODO -- 初始化
        initTabbarButton(context);

        typedArray.recycle();

        this.setFocusable(true);
        this.setClickable(true);
    }

    private void initTabbarButton(Context context) {
        LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        view = (LinearLayout) inflater.inflate(R.layout.view_tabbar_button, this, true);
        imageView = view.findViewById(R.id.image4TabbarButton);
        textView = view.findViewById(R.id.text4TabbarButton);
        //将自定义的属性值传递给view
        imageView.setBackgroundResource(image);
        textView.setTextColor(textColors);
        textView.setText(text);
    }

    @Override
    public void setSelected(boolean selected) {
        super.setSelected(selected);
        imageView.setSelected(selected);
        textView.setSelected(selected);
    }

    @Override
    public void setPressed(boolean pressed) {
        super.setPressed(pressed);
        imageView.setPressed(pressed);
        textView.setPressed(pressed);
    }
}
